package com.wenx.ftpcamel;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.ModelCamelContext;

/**
 * 本地路径下文件传送经过activemq
 */
public class FileDemo2  {
    //move=${date:now:yyyyMMdd}/${file:name} 不增加move文件会一直在，此时camel会一直在传送之前传送过的文件
    //&antInclude=*.txt,w*.txt,es*,*e.* 文件名符合这些规则，其中之一即可
    //&antExclude=w* 文件名不符合以下规则
    //ftpClient.controlEncoding=utf-8 解决ftp 中文目录不识别，中文文件名乱码问题
    public static void main(String[] args) throws Exception {
        ModelCamelContext camelContext = new DefaultCamelContext();
        camelContext.start();
        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("sftp://10.10.5.213:49622/张三?username=root&password=ufsoft*123" +
                        "&delete=false" +
//                        "&move=${date:now:yyyyMMdd}/${file:name}" +
                        "&moveFailed=./error" +
                        "&antInclude=*.txt,w*.txt," +
//                        "&antExclude=w*" +
                        "")
                        .doTry()
                        .process(new Processor() {
                            public void process(Exchange exchange) throws Exception {
                                System.out.println("*******************");
                                System.out.println("文件名 ： " + exchange.getIn().getHeader(Exchange.FILE_NAME));
                                System.out.println("绝对路径 ： " + exchange.getIn().getHeader("CamelFileAbsolutePath"));
                                System.out.println("最后修改时间 ： " + exchange.getIn().getHeader("CamelFileLastModified"));
                                System.out.println("文件大小： " + exchange.getIn().getHeader("CamelFileLength"));
                            }
                        })
                        .to("file:D://camelFile//fileB//demo")
                        .doCatch(Exception.class)
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                Object object = exchange.getProperty("CamelExceptionCaught");
                                System.out.println("object = " + object);
                            }
                        });
            }
        });
        synchronized (FileDemo2.class) {
            FileDemo2.class.wait();
        }
    }
}
